//
//  AddAddressViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/7/24.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "AddAddressViewController.h"
#import "BRAddressPickerView.h"
#import "AddressItemModel.h"

@interface AddAddressViewController ()

@property (weak, nonatomic) IBOutlet UITextField *addressNameTF;
@property (weak, nonatomic) IBOutlet UITextField *addressPhoneTF;

@property (weak, nonatomic) IBOutlet UIView *addressSelectView;
@property (weak, nonatomic) IBOutlet UILabel *addressLab;

@property (weak, nonatomic) IBOutlet UITextField *addressDescTF;

@property (weak, nonatomic) IBOutlet UISwitch *addressDefaultSwitch;

@property (nonatomic, assign) BOOL isAgree;

@property (nonatomic, copy) NSArray *selectedAddressArray;

@property (nonatomic, copy) AddressItemModel *selectModel;

@end

@implementation AddAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self prepareData];
    
    if (self.defaultModel) {
        [self loadDefaultData];
    }
}

- (void)request_AddressUpdate {
    
    NSMutableDictionary *dic = @{}.mutableCopy;

    [dic setValue:self.addressNameTF.text forKey:@"name"];
    [dic setValue:self.addressPhoneTF.text forKey:@"phone"];
    
    [dic setValue:self.selectModel.provence forKey:@"province"];
    [dic setValue:self.selectModel.city forKey:@"city"];
    [dic setValue:self.selectModel.area forKey:@"area"];
    [dic setValue:self.addressDescTF.text forKey:@"address"];
    
    NSString *isdefault = self.addressDefaultSwitch.on?@"1":@"0";
    [dic setValue:isdefault forKey:@"isUse"];
    
    [dic setValue:self.defaultModel.addressID forKey:@"id"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_ADDRESS_UPDATE];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            [SVProgressHUD showWithStatus:[data valueForKey:@"msg"]];
            [SVProgressHUD dismissWithDelay:1];
            
            [self.navigationController popViewControllerAnimated:YES];

        } else {
            
        }
        
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
}

- (void)request_addAddress {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.addressNameTF.text forKey:@"name"];
    [dic setValue:self.addressPhoneTF.text forKey:@"phone"];

    [dic setValue:self.selectModel.provence forKey:@"province"];
    [dic setValue:self.selectModel.city forKey:@"city"];
    [dic setValue:self.selectModel.area forKey:@"area"];
    [dic setValue:self.addressDescTF.text forKey:@"address"];
    
    NSString *isdefault = self.addressDefaultSwitch.on?@"1":@"0";
    [dic setValue:isdefault forKey:@"isUse"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_ADDRESS_ADD];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                
                    
                [SVProgressHUD showSuccessWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];

                [self.navigationController popViewControllerAnimated:YES];

            } else {
                
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
            
        } else {
            
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
    }];
}

- (void)prepareData {
    
    self.selectedAddressArray = @[@"安徽省",@"合肥市",@"包河区"];
}

- (void)loadDefaultData {
    
    self.title = @"修改收货地址";
    
    self.addressNameTF.text = self.defaultModel.addressName;
    self.addressPhoneTF.text = self.defaultModel.addressPhone;
    
    self.addressLab.text = [NSString stringWithFormat:@"%@-%@-%@",self.defaultModel.provence,self.defaultModel.city,self.defaultModel.area];
    self.addressDefaultSwitch.on = self.defaultModel.isDefault;

    self.addressDescTF.text = self.defaultModel.addressDesc;
    
    self.selectModel.provence = _defaultModel.provence;
    self.selectModel.city = _defaultModel.city;
    self.selectModel.area = _defaultModel.area;
    
    self.isAgree = self.defaultModel.isDefault;
    
    self.selectedAddressArray =@[_defaultModel.provence,_defaultModel.city,_defaultModel.area];

}

- (IBAction)morenBrnClicked:(id)sender {
    self.isAgree = !self.isAgree;
    if (!self.isAgree) {

    } else {

    }
}

- (IBAction)morenLabClicked:(id)sender {
    
    self.isAgree = !self.isAgree;
    self.addressDefaultSwitch.on = self.isAgree;

}

- (void)initView {
    
    self.title = @"新增收货地址";    
    [self.addressSelectView addGestureTapTarget:self action:@selector(showAddressPicker)];
}

/*
 [dic setValue:self.addressNameTF.text forKey:@"name"];
 [dic setValue:self.addressPhoneTF.text forKey:@"phone"];
 
 [dic setValue:self.selectModel.provence forKey:@"province"];
 [dic setValue:self.selectModel.city forKey:@"city"];
 [dic setValue:self.selectModel.area forKey:@"area"];
 [dic setValue:self.addressDescTF.text forKey:@"address"];
 */

- (IBAction)saveAddress:(id)sender {

    if (!self.addressNameTF.text.length) {
        
        [self alert:@"请输入收货人姓名"];
    } else {
        
        if (!self.addressPhoneTF.text.length) {
            
            [self alert:@"请输入收货人手机号码"];
        } else {
            
            if (!self.selectModel.provence.length) {
                
                [self alert:@"请选择“所在地区"];
            } else {
                
                if (!self.addressDescTF.text.length) {
                    
                    [self alert:@"请输入街道门牌等信息"];
                } else {
                    
                    if (self.defaultModel) {
                        [self request_AddressUpdate];
                    } else {
                        [self request_addAddress];
                    }
                }
            }
        }
    }
}

-(void)showAddressPicker{
    
    [self.view endEditing:YES];
    
    [BRAddressPickerView showAddressPickerWithDefaultSelected:self.selectedAddressArray resultBlock:^(BRProvinceModel *province, BRCityModel *city, BRAreaModel *area) {
        
        self.addressLab.text = [NSString stringWithFormat:@"%@-%@-%@",province.name,city.name,area.name];
        self.selectModel.provence = province.name;
        self.selectModel.city = city.name;
        self.selectModel.area = area.name;
    }];
    
}

- (AddressItemModel *)selectModel {
    if (!_selectModel) {
        _selectModel = [[AddressItemModel alloc] init];
    }
    return _selectModel;
}
@end
